class ViewsController < ApplicationController

   def index
      @views = Room.find(:all, :conditions => "id > 0", :include => :machines)
      @views0 = Room.find(:all, :conditions => "id = 0", :include => :machines)
      @incident = Incident.find_by_sql("select machine_id from incidents where resolution_date IS NULL group by machine_id")
   end

   def trash_machine
      machine_id = params[:machine]
      Machine.delete(machine_id)
      
      render :update do |page|
          page.hide machine_id
          page.replace_html "#{machine_id}", "" 
      end          
  end
  
   def change_room
      room_id = params[:room]
      machine_id = params[:machine]
      Machine.update_all("room_id = #{room_id}", [ "id = ?", machine_id ])
      @machine = Machine.find(params[:machine])
      @lien_ping = '/monitorings/ping_machine_manuel/' + @machine.id.to_s
      @lien_delete = '/views/destroy_by_view/' + @machine.id.to_s
      @incidents = Incident.find_by_sql("select count(*) As resultat from incidents where resolution_date IS NULL AND machine_id = #{machine_id}")
      m_inc = ""
      @incidents.each do |incident|
         m_inc = "<td><a href='/machines/incidentlist/#{machine_id}'><img src='/images/warning.png' /></a></td>"
      end
      
      render :update do |page|
          page.hide machine_id
          page.insert_html :bottom, "room_#{room_id}", 
            "<div id='pc'><table class='table1'><td><img src='images/computer1.png'><br />#{@machine.machine_name}<td>#{@machine.ip_adr}</td><td>#{link_to image_tag('preview.png'), machine_path(@machine.id)}</td> <td>#{link_to image_tag('reseau.png'), @lien_ping} </td> <td>#{link_to image_tag("delete.png"), @lien_delete}</td>#{m_inc}</table></div> <br />"
      end    
  end
  
  
  
    def destroy_by_view
      machine_id = params[:id]
      @machine = Machine.find(machine_id)
      @machine.destroy
    Incident.delete_all("machine_id = #{machine_id}")
      
      respond_to do |format|
      format.html { redirect_to(views_url) }
      format.xml {head :ok}
      end
    end
end
